package com.engine.kq.wfset.action;

import com.engine.kq.biz.KQFLowEventLogBiz;
import com.engine.kq.biz.KQFlowActiontBiz;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.bean.SplitBean;
import com.engine.kq.wfset.util.KQFlowDutyUtil;
import com.engine.kq.wfset.util.KQFlowOvertimeUtil;
import com.engine.kq.wfset.util.SplitActionUtil;
import com.engine.kq.wfset.util.SplitDutyActionUtil;
import com.google.common.collect.Maps;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.workflow.WorkflowComInfo;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 值班生成调休action
 */
public class KqDutyAction extends BaseBean implements Action {
    private KQLog kqLog = new KQLog();

    @Override
    public String execute(RequestInfo request) {
        this.writeLog("KqDutyAction", "do action on request:" + request.getRequestid());
        String requestid = request.getRequestid();
        kqLog.info("do KqDutyAction on requestid:" + requestid);
        int requestidInt = Util.getIntValue(requestid, 0);

        String workflowid = request.getWorkflowid();
        String formid = new WorkflowComInfo().getFormId(workflowid);
        KQFLowEventLogBiz kqfLowEventLogBiz = new KQFLowEventLogBiz();
        String logKey = "|key|requestid|" + requestid;

        try {
            List<SplitBean> splitBeans = new ArrayList<SplitBean>();

            KQFlowActiontBiz kqFlowActiontBiz = new KQFlowActiontBiz();
            KQFlowDutyUtil kqFlowDutyUtil = new KQFlowDutyUtil();
            ResourceComInfo rci = new ResourceComInfo();
            DateTimeFormatter datetimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            RecordSet rs = new RecordSet();
            String proc_set_sql = "select * from kq_att_proc_set where field001 = ? and field002 = ? ";
            rs.executeQuery(proc_set_sql, workflowid, formid);

            Map<String, Object> eventLogMap = Maps.newHashMap();
            eventLogMap.put("proc_set_sql", proc_set_sql);
            eventLogMap.put("workflowid", workflowid);
            eventLogMap.put("formid", formid);

            if (rs.next()) {
                String proc_set_id = rs.getString("id");
//得到这个考勤流程设置是否使用明细
                String usedetails = rs.getString("usedetail");
                int kqtype = Util.getIntValue(rs.getString("field006"));
                Map<String, String> map = new HashMap<>();
                if (requestidInt > 0) {
                    map.put("requestId", "and t.requestId = " + requestidInt);
                }
                Map<String, String> sqlMap = kqFlowActiontBiz.handleSql(proc_set_id, usedetails, requestidInt, kqtype, map);
                Map<String, String> result = kqFlowDutyUtil.handleKQDutyAction(sqlMap, splitBeans, datetimeFormatter, Util.getIntValue(workflowid), requestidInt, rci);
                if (!result.isEmpty()) {
                    String error = Util.null2String(result.get("message"));
                    request.getRequestManager().setMessageid("666" + request.getRequestid() + "999");
                    request.getRequestManager().setMessagecontent(error);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
// 生成调休 在归档的时候再单独处理下值班规则第二种情况
            SplitDutyActionUtil.handleOverTimeAction(splitBeans, requestid);
        } catch (Exception e) {
            writeLog(e);
            request.getRequestManager().setMessageid("11111" + request.getRequestid() + "22222");
            request.getRequestManager().setMessagecontent("值班流程【值班时间转为可调休时间action】报错，请联系管理员！");
            StringWriter errorsWriter = new StringWriter();
            e.printStackTrace(new PrintWriter(errorsWriter));
            kqLog.info("值班流程【值班时间转为可调休时间action】报错，请联系管理员！" + errorsWriter.toString());
            Map<String, Object> eventLogMap = Maps.newHashMap();
            eventLogMap.put("action_error", errorsWriter.toString());
            String uuid = kqfLowEventLogBiz.logEvent("request|Creatorid|" + request.getCreatorid(), eventLogMap, "KqPaidLeaveAction|触发生成调休action" + logKey);
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }

}